//
//  AuthorityManager.m
//  TBC_Harvest
//
//  Created by 李希朋 on 2017/4/17.
//  Copyright © 2017年 sdgh. All rights reserved.
//

#import "AuthorityManager.h"
#import <Photos/Photos.h>
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import<AssetsLibrary/AssetsLibrary.h>

@implementation AuthorityManager



+(void)getPhotoAuthorityWithAgreeAuthority:(AgreeAuthority)agreeAuthority
{
    ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
    
    if(author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied)
    {
        UIAlertView *photoAlbum = [[UIAlertView alloc]initWithTitle:nil message:@"请在iPhone的“设置-隐私-相册”选项中，允许“收获”访问你的相册。" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [photoAlbum show];
        
        agreeAuthority(NO);
    }
    else if (author == ALAuthorizationStatusNotDetermined)
    {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
            dispatch_async(dispatch_get_main_queue(), ^{
                
                agreeAuthority(status==PHAuthorizationStatusAuthorized);
            });
        }];
    }
    else
    {
        agreeAuthority(YES);
    }
}



+(void)getCameraAuthorityWithAgreeAuthority:(AgreeAuthority)agreeAuthority
{
    AVAuthorizationStatus authStatus =  [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    {
        UIAlertView *photoAlbum = [[UIAlertView alloc]initWithTitle:nil message:@"请在iPhone的“设置-隐私-相机”选项中，允许“收获”访问你的相机。" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [photoAlbum show];
        
        agreeAuthority(NO);
    }
    else if (authStatus == AVAuthorizationStatusNotDetermined)
    {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
           
            dispatch_async(dispatch_get_main_queue(), ^{
                agreeAuthority(granted);
            });
        }];
    }
    else
    {
        agreeAuthority(YES);
    }
}

+(void)getMicrophoneAuthorityWithAgreeAuthority:(AgreeAuthority)agreeAuthority
{
    AVAuthorizationStatus author = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    
    if(author == AVAuthorizationStatusRestricted || author ==AVAuthorizationStatusDenied)
    {
        UIAlertView *photoAlbum = [[UIAlertView alloc]initWithTitle:nil message:@"请在iPhone的“设置-隐私-相册”选项中，允许“收获”访问你的麦克风" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [photoAlbum show];
        
        agreeAuthority(NO);
    }
    else if (author == AVAuthorizationStatusNotDetermined)
    {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
           
            dispatch_async(dispatch_get_main_queue(), ^{
                agreeAuthority(granted);
            });
            
        }];
    }
    else
    {
        agreeAuthority(YES);
    }
}

@end
